//
//  registerView.m
//  Follow Me
//
//  Created by apple on 2017/4/6.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "registerView.h"
#import "Users+CoreDataClass.h"


@interface registerView ()<UITextFieldDelegate>

@property (nonatomic,strong) registerView *logs;
@property (nonatomic)Users *userInfo;
//@property(nonatomic, strong)UITabBar *tabBar;



@end


@implementation registerView


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.noticeLabel.hidden = YES;
    self.registerName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"User Name" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    self.registerPassword.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"Password" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    
    self.registerName.delegate = self;
    self.registerPassword.delegate = self;
    //改变tabBar颜色
//    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor lightGrayColor];
//    view.frame = self.tabBar.bounds;
//    [[UITabBar appearance] insertSubview:view atIndex:0];
    //&&
//    [[UITabBar appearance]setBackgroundColor:[UIColor lightGrayColor]];
    

    
    
}



//点击空白处隐藏键盘objective-c
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.registerName endEditing:YES];
    [self.registerPassword endEditing:YES];
}


//完成输入后return隐藏键盘objective-c
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}



- (IBAction)cancelRegisterAndBackToLogin:(id)sender {
    
    self.logs = [[NSBundle mainBundle] loadNibNamed:@"loginView" owner:self options:nil].firstObject;
    [self addSubview:self.logs];
    
    
}

- (IBAction)finishRegisterAndBackToLogin:(id)sender {
    
    self.noticeLabel.alpha =1.0f;
    if (self.registerName.text.length>0&&self.registerPassword.text.length>0){
       NSArray *registerName = [Users findByAttribute:@"userName" withValue: self.registerName.text];
        if (registerName.count ==0)
        {
        Users *userInfo = [Users createEntity];
    userInfo.userName = self.registerName.text;
    userInfo.password = self.registerPassword.text;
     [[NSManagedObjectContext MR_defaultContext] MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
         NSLog(@"%d",success);
     }];
        NSArray *personal = [Users findAll];
        NSLog(@"%@ %@",userInfo.userName,userInfo.password);
        NSLog(@"%@",personal);
        
        //        [userInfo deleteEntity];
    //jump to login
    self.logs = [[NSBundle mainBundle] loadNibNamed:@"loginView" owner:self options:nil].firstObject;
    [self addSubview:self.logs];
    }else{
        self.noticeLabel.layer.cornerRadius = 10;
        self.noticeLabel.layer.masksToBounds = YES;
        self.noticeLabel.hidden = NO;
        [UIView animateWithDuration:2.5 animations:^{
            
            self.noticeLabel.alpha = 0.0f;
            
        }completion:^(BOOL finished) {
            self.noticeLabel.hidden = YES;
        }];
        
        self.registerName.text = @"";
        self.registerPassword.text = @"";
        [self.registerName endEditing:YES];
        [self.registerPassword endEditing:YES];
        
    }
        
    }
    
}









/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
